package com.ky.course.service.impl;

import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.course.entity.Follow;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.course.mapper.FollowMapper;
import com.ky.course.service.FollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 粉丝关注表 服务实现类
 * </p>
 *
 * @author zpa
 * @since 2024-06-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Autowired
    private FollowMapper followMapper;




    /**
     * 顾晓关注用户
     * @param token
     * @param followingId
     * @return
     */
    @Override
    public R unfollowUser(String token, Integer followingId) {
       return  unfollow(token,followingId,1);
    }

    /**
     * 取消关注老师
     * @param token
     * @param followingId
     * @return
     */
    @Override
    public R unfollowTeacher(String token, Integer followingId) {
        return unfollow(token,followingId,2);
    }

    /**
     * 关乎用户
     * @param token
     * @param teacherId
     * @return
     */
    @Override
    public R followTeacher(String token, Integer teacherId) {
        return  following(token,teacherId,2);
    }

    @Override
    public R followUser(String token, Integer userId) {
       return following(token,userId,1);
    }

    @Override
    public R followArticle(String token, Integer article) {
        return following(token,article,3);
    }

    @Override
    public R unfollowArticle(String token, Integer articleId) {
        return unfollow(token,articleId,3);
    }

    public R unfollow(String token, Integer followingId,int followType){
        Integer userId = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        // 检查 用户是否关注此用户

        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id",userId)
                .eq("following_id",followingId)
                .eq("follow_type",followType); //1表示该用户关注的是某个用户

        List<Follow> follows = followMapper.selectList(queryWrapper);

        if( follows==null || follows.isEmpty()){
            return R.fail("您尚未关注该用户");
        }
        Follow follow = follows.get(0);
        int result = followMapper.deleteById(follow.getFollowId());

        return result > 0 ? R.ok() :R.fail("取消关注失败");
    }

    /**
     * 关注用户或教师
     * 1为关注用户
     * 2为关注教师
     * 3 为关注文章
     * @param  token
     * @param  followingId
     * @param  followType
     * @return
     */

    public R following(String token,Integer followingId,Integer followType ) {
        Integer userId = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        // 检查用户是否已经关注
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id",userId)
                .eq("following_id",followingId)
                .eq("follow_type",followType);
        List<Follow> follows = followMapper.selectList(queryWrapper);
        if(follows!=null && !follows.isEmpty()){
            return R.fail("您已关注");
        }


        Follow follow = new Follow();
        follow.setFollowerId(userId)
                .setFollowingId(followingId)
                .setFollowType(followType)
                .setCreatedTime(new Date());

        int result = followMapper.insert(follow);
        return result>0 ? R.ok() : R.fail("关注失败");

    }
}
